package com.ellricky.baselib.ui.teacher

import android.content.Intent
import android.os.Build
import android.view.View
import androidx.annotation.RequiresApi
import com.andr.common.util.EmptyUtil
import com.andr.common.util.ToastUtil
import com.ellricky.baselib.BaseConstant
import com.ellricky.baselib.R
import com.ellricky.baselib.cache.LocalCacheUtil
import com.ellricky.baselib.entity.new.StuEvaluate
import com.ellricky.baselib.http.HttpUtils
import com.ellricky.baselib.ui.base.BaseTakePhotoActivity
import com.ellricky.baselib.util.*
import kotlinx.android.synthetic.main.tc_activity_stu_evaluate.*
import org.devio.takephoto.model.TResult

class EvaluateStudentActivity : BaseTakePhotoActivity() {

    private lateinit var stuId: String
    private lateinit var stuName: String
    private lateinit var courseListId: String
    private lateinit var stuEvaluate: StuEvaluate
    private var picPath: String = ""

    override fun getContentLayout(): Int {
        return R.layout.tc_activity_stu_evaluate
    }

    override fun initUIView() {
        super.initUIView()
        stuId = intent.getStringExtra(BaseConstant.KEY_ID) ?: ""
        stuName = intent.getStringExtra(BaseConstant.KEY_NAME) ?: ""
        courseListId = intent.getStringExtra(BaseConstant.KEY_IDs) ?: ""

        mTvTitle.text = getString(R.string.tc_student_evaluate)
        btnConfirm.setOnClickListener(this)
        btnUnFinish.setOnClickListener(this)
        ivPic.setOnClickListener(this)
        tvUserName.text = stuName
        loadDetail()
    }

    private fun loadDetail() {
        val url = EAppUtil.getApiHost()
        val paramMap = hashMapOf<String, String>()
        paramMap["action"] = "teacherJobDetail"
        paramMap["course_list_id"] = courseListId
        paramMap["member_id"] = stuId
        paramMap["eid"] = LocalCacheUtil.getUid()

        HttpUtils.getInstance()
            .postFrom(url, paramMap, object : NewEntityCallback<StuEvaluate>(this, false) {
                override fun onSuccess(data: StuEvaluate, var2: String?) {
                    stuEvaluate = data
                    picPath = data.job_image
                    zzlBar.setRate(data.job_focus.toInt())
                    gtlBar.setRate(data.job_talk.toInt())
                    txrzBar.setRate(data.job_cognize.toInt())
                    bxlBar.setRate(data.job_expressively.toInt())
                    workBar.setRate(data.job_result.toInt())
                    if (data.job_image.isNotEmpty()) {
                        GlideUtil.into(this@EvaluateStudentActivity, data.job_image, ivPic)
                    }
                }
            })
    }

    override fun onClickView(v: View) {
        super.onClickView(v)
        when (v.id) {
            R.id.btnConfirm -> {
                uploadEva(true)
            }
            R.id.btnUnFinish -> {
                uploadEva(false)
            }
            R.id.ivPic -> {
                EDialogUtil.showTakePhotoDialog(this, this)
            }
        }
    }

    private fun uploadEva(isFinish: Boolean) {
        if (picPath.isEmpty()) {
            ToastUtil.show(this, "请上传作品")
            return
        }
        val nlFirst = zzlBar.rate.replace("%", "")
        val nlSecond = gtlBar.rate.replace("%", "")
        val nlThird = txrzBar.rate.replace("%", "")
        val nlFour = bxlBar.rate.replace("%", "")
        val nlFive = workBar.rate.replace("%", "")

        val url = EAppUtil.getApiHost()
        val paramMap = hashMapOf<String, String>()
        paramMap["action"] = "addCourseJob"
        paramMap["eid"] = LocalCacheUtil.getUid()
        paramMap["job_courselist_id"] = courseListId
        paramMap["job_member_id"] = stuId
        paramMap["job_out_dic_id"] = stuEvaluate.job_out_dic_id
        paramMap["job_image"] =
            if (picPath.startsWith("http")) picPath else FileUtil.fileToBase64(picPath)
        paramMap["job_desc"] = "无"
        paramMap["job_focus"] = nlFirst
        paramMap["job_talk"] = nlSecond
        paramMap["job_cognize"] = nlThird
        paramMap["job_expressively"] = nlFour
        paramMap["job_result"] = nlFive
        paramMap["job_is_finish"] = if (isFinish) "1" else "0"

        HttpUtils.getInstance()
            .postFrom(url, paramMap, object : NewEntityCallback<Any>(this, false) {
                override fun onSuccess(data: Any?, var2: String?) {
                    ToastUtil.show(this@EvaluateStudentActivity, "提交成功")
                    finish()
                }
            })
    }


    override fun takeSuccess(result: TResult?) {
        val picUrl = result!!.image.compressPath
        uploadPhoto(picUrl)
    }


    private fun uploadPhoto(picPath: String) {
        // 接口提交
        if (EmptyUtil.isEmpty(picPath)) {
            ToastUtil.show(this, "图片路径为空")
            return
        }
        if (!EmptyUtil.isEmpty(picPath)) {
            this.picPath = picPath
            GlideUtil.into(this, picPath, ivPic)
        }
    }
}

